/**
 * Copyright (C) 2009 alex <me@alex.0x08.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uber.netbsd.community.action;

import java.io.IOException;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.FileBean;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.Validate;
import uber.netbsd.community.dao.gallery.GalleryBuilder;
import uber.netbsd.community.dao.gallery.GalleryManager;

/**
 * Action to add new image to gallery
 * @author alex
 */
@UrlBinding("/action/AddImage")
public class AddImageActionBean extends BaseActionBean {

    /**
     *  Uploaded image
     */
    private FileBean newImage;
    
    /**
     * Image description
     */
    @Validate(required = true, minlength = 3, maxlength = 25)
    private String desc;

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public FileBean getNewImage() {
        return newImage;
    }

    public void setNewImage(FileBean newImage) {
        this.newImage = newImage;
    }

    @DefaultHandler
    public Resolution save() throws IOException {

      
        if (this.newImage != null) {

           
            GalleryManager.getInstance().saveOrUpdate(GalleryBuilder.getInstance()
                    .addImageFromStream(newImage.getInputStream())
                    .addName(desc).get());

        }

        return new RedirectResolution("/gallery/list.jsp");
    }
}
